﻿Imports System.Drawing.Graphics
Imports System.Drawing.Drawing2D
Public Class Bubbles
    Private m_Picture As New DrawablePicture(Me.BackColor)
    Private WithEvents _Timer As Timer

    ' The current drawing attributes.
    Private m_CurrentLineWidth As Integer = 1
    Private m_CurrentForeColor As Color = Color.Black
    Private m_CurrentFillColor As Color = Color.White

    ' The object we are currently drawing.
    Private m_NewDrawable As Drawable
    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        _Timer = New Timer
        _Timer.Enabled = False
    End Sub

    Private Sub picCanvas_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If chkAdd.Checked Then
                ' Start drawing an ellipse.
                m_NewDrawable = New DrawableEllipse(m_CurrentForeColor, m_CurrentFillColor, m_CurrentLineWidth, e.X, e.Y, e.X, e.Y)
                m_NewDrawable.Index = m_Picture.Drawables.Count
                m_Picture.Add(m_NewDrawable)
                AddHandler picCanvas.MouseMove, AddressOf NewDrawable_MouseMove
                AddHandler picCanvas.MouseUp, AddressOf NewDrawable_MouseUp
            Else
                m_Picture.SelectObjectAt(e.X, e.Y)
            End If
        End If
        picCanvas.Invalidate()
    End Sub

    ' If we have an object selected, move it.
    Private Sub picCanvas_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseMove
        ' Only move if the left button is down.
        If e.Button = Windows.Forms.MouseButtons.Left Then
            'If m_Picture.SelectedDrawable IsNot m_Picture.SelectObjectAtToConnect(e.X, e.Y) Then
            '    picCanvas.Invalidate()
            'Else
            '    ' Move it.
            m_Picture.MoveSelectedDrawableToMouse(e.X, e.Y)
            'End If

            ' Redraw to show the new position.
            picCanvas.Invalidate()

        Else

        End If
    End Sub

    ' On mouse move, continue drawing.
    Private Sub NewDrawable_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        ' Update the new line's coordinates.
        'm_NewDrawable.NewPoint(e.X, e.Y)
        m_NewDrawable.NewPoint(m_NewDrawable.X1 + 40, m_NewDrawable.Y1 + 40) ' e.X, e.Y)
       

        ' Redraw to show the new line.
        picCanvas.Invalidate()
    End Sub

    ' On mouse up, finish drawing the new object.
    Private Sub NewDrawable_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        ' No longer watch for MouseMove or MouseUp.
        RemoveHandler picCanvas.MouseMove, AddressOf NewDrawable_MouseMove
        RemoveHandler picCanvas.MouseUp, AddressOf NewDrawable_MouseUp

        ' See if the new object is empty (e.g. a zero-length line).
        If m_NewDrawable.IsEmpty() Then
            ' Discard this object.
            m_Picture.Remove(m_NewDrawable)
        Else
            '' If it's a new image, get the image file.
            'If TypeOf (m_NewDrawable) Is DrawableImage Then
            '    If ofdImage.ShowDialog() = Windows.Forms.DialogResult.OK Then
            '        ' Discard this object.
            '        Dim drawable_image As DrawableImage = DirectCast(m_NewDrawable, DrawableImage)
            '        drawable_image.Picture = New Bitmap(ofdImage.FileName)
            '    Else
            '        ' Discard this object.
            '        m_Picture.Remove(m_NewDrawable)
            '    End If
            'End If
        End If

        ' We're no longer working with the new object.
        m_NewDrawable = Nothing

        ' Redraw.
        picCanvas.Invalidate()
    End Sub

    Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picCanvas.Paint
        m_Picture.Draw(e.Graphics)
    End Sub

    Private Sub btnBalance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBalance.Click
        Dim dr As Drawable = m_Picture.Drawables(0)

        Debug.Print(dr.X1)
        Debug.Print(dr.GetCentre.X)
        Dim iMainCentre As Location = dr.GetCentre

        dr = m_Picture.Drawables(1)
        Dim iMoveCentre As Location = dr.GetCentre
        m_Picture.SelectObject(m_Picture.Drawables(1))

        If (iMoveCentre.X > iMainCentre.X) And (iMoveCentre.Y < iMainCentre.Y) Then ' Q2
            iMoveCentre.X = iMainCentre.X + (iMoveCentre.X - iMainCentre.X) / 2
            iMoveCentre.Y = iMainCentre.Y + (iMoveCentre.Y - iMainCentre.Y) / 2
        ElseIf (iMoveCentre.X < iMainCentre.X) And (iMoveCentre.Y < iMainCentre.Y) Then ' Q1
            iMoveCentre.X = iMoveCentre.X + (iMainCentre.X - iMoveCentre.X) / 2
            iMoveCentre.Y = iMoveCentre.Y + (iMainCentre.Y - iMoveCentre.Y) / 2
        ElseIf (iMoveCentre.X < iMainCentre.X) And (iMoveCentre.Y > iMainCentre.Y) Then ' Q3
            iMoveCentre.X = iMoveCentre.X + (iMainCentre.X - iMoveCentre.X) / 2
            iMoveCentre.Y = iMoveCentre.Y + (iMainCentre.Y - iMoveCentre.Y) / 2
        ElseIf (iMoveCentre.X > iMainCentre.X) And (iMoveCentre.Y > iMainCentre.Y) Then ' Q4
            iMoveCentre.X = iMoveCentre.X + (iMainCentre.X - iMoveCentre.X) / 2
            iMoveCentre.Y = iMoveCentre.Y + (iMainCentre.Y - iMoveCentre.Y) / 2
        Else
            iMoveCentre.X = iMainCentre.X + (iMainCentre.X - iMoveCentre.X) / 2
        End If

        m_Picture.MoveSelectedDrawableBYCentre(iMoveCentre)
        picCanvas.Invalidate()
        Exit Sub

        iMainCentre.X = dr.X1
        iMainCentre.Y = dr.Y1
        For xx As Integer = iMainCentre.X To iMainCentre.X + 2
            m_Picture.MoveSelectedDrawableBYCentre(iMainCentre)
            picCanvas.Invalidate()
        Next
        picCanvas.Invalidate()
        
        
    End Sub

    Private Sub _Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles _Timer.Tick

    End Sub
End Class
